package com.ptswitch.material.services;

import org.apache.tapestry5.NullFieldStrategy;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.ioc.annotations.Local;
import org.apache.tapestry5.ioc.annotations.SubModule;
import org.apache.tapestry5.services.Dispatcher;
import org.apache.tapestry5.services.RequestExceptionHandler;
import org.hxzon.tapestry5.services.HxzonTapestryModule;
import org.hxzon.tapestry5.util.EmptyNullFieldStrategy;
import org.hxzon.tapestry5.util.MyAssetDispatcher;
import org.hxzon.tapestry5.util.MyRequestExceptionHandler;

import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.WebUtil;

@SubModule({ HxzonTapestryModule.class })
public class AppModule {
    public static void bind(ServiceBinder binder) {
        binder.bind(Dispatcher.class, MyAssetDispatcher.class).withId("MyAssetDispatcher");
        binder.bind(RequestExceptionHandler.class, MyRequestExceptionHandler.class).withId("MyRequestExceptionHandler");
        binder.bind(WebUtil.class);
        binder.bind(MaterialUtil.class);
    }

    public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration) {
//		configuration.add(SymbolConstants.APPLICATION_VERSION, APPLICATION_VERSION);
        configuration.add(SymbolConstants.COOKIE_MAX_AGE, "14 d");
//		configuration.add(ClientResourcesConstants.JAVASCRIPT_STACK, JQueryClientResourcesConstants.JAVASCRIPT_STACK_JQUERY);
//		configuration.add(ClientResourcesConstants.DISABLED_FORM_AUTOFOCUS, "false");

    }

//    @Contribute(PageRenderLinkTransformer.class)
//    @Primary
//    public static void contributeURLRewriter(OrderedConfiguration<PageRenderLinkTransformer> configuration) {
//        configuration.add("Rule-webinf-css-js", new SafeAndCssUrlRewriter(Index.class.getSimpleName()));
//    }

    public static void contributeNullFieldStrategySource(MappedConfiguration<String, NullFieldStrategy> configuration) {
        configuration.override("default", new EmptyNullFieldStrategy());
    }

    public static void contributeIgnoredPathsFilter(Configuration<String> configuration) {
        configuration.add("/zkau/web/.*");
        configuration.add("/zkau/.*");
        configuration.add("/zkau");
//        configuration.add(".*\\.css");
//        configuration.add(".*\\.js");
    }

    public static void contributeMasterDispatcher(OrderedConfiguration<Dispatcher> configuration,

    @InjectService("MyAssetDispatcher") Dispatcher myAssetDispatcher) {
        configuration.add("MyAsset", myAssetDispatcher, "before:Asset");
    }

    public static void contributeServiceOverride(MappedConfiguration<Class<?>, Object> configuration, @Local RequestExceptionHandler overrideRequestExceptionHandler) {
        configuration.add(RequestExceptionHandler.class, overrideRequestExceptionHandler);
    }

}
